package leetcode.leetcode150;

import java.util.Stack;

/**
 * @author czt
 * @version 1.0
 * @since 2024/12/20
 *
 * 逆波兰表达式求值
 */
public class Leetcode150 {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            switch (token) {
                case "+":
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    stack.push(a + b);
                    break;
                case "-":
                    Integer b1 = stack.pop();
                    Integer a1 = stack.pop();
                    stack.push(a1 - b1);
                    break;
                case "*":
                    Integer b2 = stack.pop();
                    Integer a2 = stack.pop();
                    stack.push(a2 * b2);
                    break;
                case "/":
                    Integer b3 = stack.pop();
                    Integer a3 = stack.pop();
                    stack.push(a3 / b3);
                    break;
                default: stack.push(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }

}
